<?php
class quarkresponse{
	protected $id,$user,$quark,$jobsolution,$createdAt,$updatedAt;
	
	function __construct($id,$userid,$quarkid,$jobsolutionid,$createdAt,$updatedAt){
		$this->id = $id;
		$this->user = user::getUserById($userid);
		$this->quark = quark::getQuarkById($quarkid);
		$this->jobsolution = jobsolution::getJobSolutionById($jobsolutionid);
		$this->createdAt = $createdAt;
		$this->updatedAt = $updatedAt;
	}
	
	/*GETTTERS*/
	public function getId(){
		return $this->id;	
	}
	
	public function getUser(){
		return $this->user;	
	}

	public function getQuark(){
		return $this->quark;	
	}

	public function getJobSolution(){
		return $this->jobsolution;	
	}
	
	public function getCreatedAt(){
		return $this->createdAt;	
	}

	public function getUpdatedAt(){
		return $this->updatedAt;	
	}

	public function setUser($var){
		$this->user = $var;
	}

	public function setQuark($var){
		$this->quark = $var;
	}

	public function setJobSolution($var){
		$this->jobsolution = $var;
	}


	public static function getQuarkResponseByUserId($userid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$query = "SELECT id, user_id, quark_id,job_solution_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM quarkresponse WHERE user_id =".$userid."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;
	}

	public static function getQuarkResponseById($id){
		$conn = new dbmodifier();
		$id = $conn->db_connection->real_escape_string($id);
		$query = "SELECT id, user_id, quark_id,job_solution_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM quarkresponse WHERE id =".$id."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();	
			$var = new quarkresponse($id,$row['user_id'],$row['quark_id'],$row['job_solution_id'],
					$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;			
	}
	
	public static function createQuarkResponse($userid,$quarkid,$jobsolutionid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$quarkid = $conn->db_connection->real_escape_string($quarkid);
		$query = "INSERT INTO quarkresponse (user_id,quark_id,job_solution_id,created_at,updated_at) " .
				 " VALUES ('".$userid."','".$quarkid."','".jobsolutionid.
				"', FROM_UNIXTIME(".time()."),FROM_UNIXTIME(".time()."))";
		$id = $conn->updateSQLKey($query);
		$conn->execute();
		$var = new quarkresponse($id,$userid,$quarkid,$jobsolutionid,time(),time());
		return $var;		
	}
	
	public function save(){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($this->user->getId());
		$quarkid = $conn->db_connection->real_escape_string($this->quark->getId());
		$jobsolutionid = $conn->db_connection->real_escape_string($this->jobsolution->getId());		
		$query = "UPDATE quarkresponse SET user_id='".$userid."'";
		$query = $query.", quark_id='".$quarkid."'";
		$query = $query.", job_solution_id='".$jobsolutionid."'";		
		$query = $query.", updated_at= FROM_UNIXTIME(".time().")";		
		$query = $query." WHERE id=".$this->id;
		$conn->updateSQL($query);
		$conn->execute();	
		$this->updated_at = time();		
	}
	
	public function delete(){
		$conn = new dbmodifier();
		$query = "DELETE FROM quarkresponse WHERE id=".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}
	
	public function deleteByQuarkId($qid){
		$conn = new dbmodifier();
		$query = "DELETE FROM quarkresponse WHERE quark_id=".$qid;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}	
}
?>